package top.goldenyear.porpoise.system.setting.service;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import java.util.List;

import com.easy.query.solon.annotation.Db;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.v7.core.text.StrUtil;
import org.noear.solon.annotation.Component;
import top.goldenyear.porpoise.common.core.exception.ErrorCode;
import top.goldenyear.porpoise.common.core.exception.ServiceException;
import top.goldenyear.porpoise.framework.web.core.model.PageResult;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;
import top.goldenyear.porpoise.framework.web.core.service.BaseService;
import top.goldenyear.porpoise.system.setting.dto.ConfigSearchReq;
import top.goldenyear.porpoise.system.setting.model.Config;
import top.goldenyear.porpoise.system.setting.model.proxy.ConfigProxy;

/**
 * 参数配置表 Service 服务层
 *
 * @author airhead
 * @since 2024-10-15
 */
@Component
@Slf4j
public class ConfigService extends BaseService<Config, ConfigProxy> {
  @Db
  private EasyEntityQuery easyEntityQuery;

  public ConfigService() {
    super(Config.class, ConfigProxy.class);
  }

  public Boolean createConfig(Config config) {
    Boolean exists = exists("configKey", config.getConfigKey(), "id", null);
    if (exists) {
      throw new ServiceException(
          ErrorCode.builder().msg(String.format("配置已存在, %s", config.getConfigKey())).build());
    }

    return save(config);
  }

  public Boolean updateConfig(Config config) {
    Boolean exists = exists("configKey", config.getConfigKey(), "id", config.getId());
    if (exists) {
      throw new ServiceException(
          ErrorCode.builder().msg(String.format("配置已存在, %s", config.getConfigKey())).build());
    }

    return updateById(config);
  }

  public Config getConfig(Integer id) {
    if (getById(id) == null) {
      throw new ServiceException(ErrorCode.builder().msg(String.format("配置不存在, %d", id)).build());
    }

    return getById(id);
  }

  public Boolean deleteConfig(Integer id) {
    return deleteById(id);
  }

  public Boolean batchDelConfig(List<Integer> ids) {
    for (Integer id : ids) {
      if (getById(id) == null) {
        throw new ServiceException(ErrorCode.builder().msg(String.format("配置不存在, %d", id)).build());
      }
    }

    return deleteByIds(ids);
  }

  public List<Config> listConfig(Config config) {
    return list(config);
  }

  public PageResult<Config> paginateConfig(QPage qPage, ConfigSearchReq searchReq) {
    if (StrUtil.isBlank(searchReq.getCategoryCode())) {
      return PageResult.of(qPage);
    }

    return paginate(
        qPage,
        configProxy -> {
          configProxy.categoryCode().eq(searchReq.getCategoryCode());
          configProxy.status().eq(searchReq.getStatus());
          configProxy.or(
              () -> {
                configProxy.name().like(searchReq.getKeyword());
                configProxy.value().like(searchReq.getKeyword());
              });
        });
  }

  @Override
  protected EasyEntityQuery easyEntityQuery() {
    return easyEntityQuery;
  }
}
